package com.gxa.wmcrm.service.Impl;

import com.gxa.wmcrm.entity.User;
import com.gxa.wmcrm.service.UserService;
import com.gxa.wmcrm.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserServiceImpl implements UserService {
    PreparedStatement ps = null;
    ResultSet rs = null;
    Connection conn = null;

    /**
     * 查询用户是否存在
     * @param user
     * @return
     */
    @Override
    public int queryUser(User user) {
        int count;
        try {
            // 调用工具类DBUtil，初始化数据库驱动，创建数据库连接
            Connection conn = DBUtil.getConn();
            String sql = "select  count(*) as n from  t_user where username = ?;";
            ps = conn.prepareStatement(sql);
            ps.setString(1, user.getUsername());
            rs = ps.executeQuery();
            rs.next();
            count = rs.getInt("n");
            if(count == 1){
                return count;
            }
        } catch (SQLException e) {
            e.getStackTrace();
        } finally {
            DBUtil.closeConn(conn, ps, null);
        }
        return 2;
    }
}
